package com.bbx.demo.nio;

import lombok.extern.slf4j.Slf4j;

import java.nio.IntBuffer;
import java.security.SecureRandom;

/**
 * buffer   读写demo
 */
@Slf4j
public class BufferTest {

    public static void main(String[] args) {
        IntBuffer intBuffer = IntBuffer.allocate(10);
        for (int i = 0; i < intBuffer.capacity(); i++) {
            int random = new SecureRandom().nextInt(20);
            intBuffer.put(random);
        }
        //反转Buffer   就是 buffer  读写切换时 必须调用该方法
        intBuffer.flip();
        while (intBuffer.hasRemaining()) {
            int i = intBuffer.get();
            log.info("intBuffer[{}]", i);
        }
    }

}
